package com.example.service.impl;

import com.example.delegator.CustomClassificationNodeDelegator;
import com.example.dto.CategoryNodeInfoViewDTO;
import com.example.result.ReturnCode;
import com.example.result.ReturnResult;
import com.example.service.IClassificationManagementService;
import com.example.vo.*;
import com.huawei.innovation.rdm.coresdk.basic.dto.ObjectReferenceParamDTO;
import com.huawei.innovation.rdm.coresdk.basic.dto.QueryParentDTO;
import com.huawei.innovation.rdm.coresdk.basic.enums.ConditionType;
import com.huawei.innovation.rdm.coresdk.basic.vo.QueryCondition;
import com.huawei.innovation.rdm.coresdk.basic.vo.QueryRequestVo;
import com.huawei.innovation.rdm.coresdk.basic.vo.RDMPageVO;
import com.huawei.innovation.rdm.xdm.delegator.ClassificationNodeDelegator;
import com.huawei.innovation.rdm.xdm.delegator.EXADefinitionDelegator;
import com.huawei.innovation.rdm.xdm.delegator.EXADefinitionLinkDelegator;
import com.huawei.innovation.rdm.xdm.dto.entity.ClassificationNodeCreateDTO;
import com.huawei.innovation.rdm.xdm.dto.entity.ClassificationNodeQueryViewDTO;
import com.huawei.innovation.rdm.xdm.dto.entity.ClassificationNodeViewDTO;
import com.huawei.innovation.rdm.xdm.dto.entity.EXADefinitionQueryViewDTO;
import com.huawei.innovation.rdm.xdm.dto.relation.EXADefinitionLinkCreateDTO;
import com.huawei.innovation.rdm.xdm.dto.relation.EXADefinitionLinkViewDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ClassificationManagementService implements IClassificationManagementService {
    private static final Logger logger= LoggerFactory.getLogger(ClassificationManagementService.class);

    @Autowired
    private ClassificationNodeDelegator classificationNodeDelegator;
    @Autowired
    private CustomClassificationNodeDelegator customClassificationNodeDelegator;
    @Autowired
    private EXADefinitionDelegator exaDefinitionDelegator;
    @Autowired
    private EXADefinitionLinkDelegator exaDefinitionLinkDelegator;
    @Override
    public ReturnResult queryByParams(String nameOrCode) {
        QueryRequestVo queryRequestVo = new QueryRequestVo();
        QueryCondition filter = new QueryCondition();
        if(nameOrCode!=null){
            filter.setJoiner("or");
            filter.addCondition("name", ConditionType.EQUAL,nameOrCode);
            filter.addCondition("businessCode",ConditionType.EQUAL,nameOrCode);
        }
        queryRequestVo.setFilter(filter);
        List<ClassificationNodeQueryViewDTO> queryList = classificationNodeDelegator.query(queryRequestVo, new RDMPageVO());
        List<CustomClassificationNodeViewVO> list = new ArrayList<>();
        for (ClassificationNodeQueryViewDTO queryViewDTO : queryList) {
            CustomClassificationNodeViewVO classificationQueryViewVO = new CustomClassificationNodeViewVO();
            BeanUtils.copyProperties(queryViewDTO,classificationQueryViewVO);
            classificationQueryViewVO.setId(queryViewDTO.getId().toString());
            list.add(classificationQueryViewVO);
        }
        return new ReturnResult(ReturnCode.GET_OK,"查询分类成功",list);
    }

    @Override
    public ReturnResult queryDetailsById(Long id) {
        //父节点
        QueryParentDTO queryParentDTO = new QueryParentDTO();
        queryParentDTO.setChildId(id);
        ClassificationNodeQueryViewDTO parent=null;
        try {
            parent = classificationNodeDelegator.getParent(queryParentDTO);
        }catch (Exception e){
            logger.info("该分类节点没有父项");
        }
        CustomClassificationNodeViewVO nodeViewVO = null;
        if(parent!=null){
            nodeViewVO = new CustomClassificationNodeViewVO();
            BeanUtils.copyProperties(parent,nodeViewVO);
            nodeViewVO.setId(parent.getId().toString());
        }
        //属性
        List<CategoryNodeInfoViewDTO> categoryNodeInfo = customClassificationNodeDelegator.getCategoryNodeInfo(id);
        List attrs = null;
        if(categoryNodeInfo!=null){
            CategoryNodeInfoViewDTO c = categoryNodeInfo.get(0);
            attrs = c.getAttrs();
        }
        //包装
        ClassificationDetailsViewVO detailsViewVO = new ClassificationDetailsViewVO();
        detailsViewVO.setParentNode(nodeViewVO);
        detailsViewVO.setAttrs(attrs);
        return new ReturnResult(ReturnCode.GET_OK,"查询详细信息成功",detailsViewVO);
    }

    @Override
    public ReturnResult checkUnique(ClassificationCheckUniqueParamsVO vo) {
        Boolean unique = customClassificationNodeDelegator.checkUnique(vo);
        return new ReturnResult(ReturnCode.BUSINESS_OK,"判断唯一性成功",unique);
    }

    @Override
    public ReturnResult createClassificationNode(ClassificationCreateFormVO vo) {
        ClassificationNodeCreateDTO dto = new ClassificationNodeCreateDTO();
        dto.setName(vo.getName());
        dto.setNameEn(vo.getNameEn());
        dto.setDescription(vo.getDescription());
        dto.setDescriptionEn(vo.getDescriptionEn());
        if(vo.getParentId()!=null&&vo.getParentId().isEmpty()){
            ObjectReferenceParamDTO parentNode = new ObjectReferenceParamDTO();
            parentNode.setId(Long.valueOf(vo.getParentId()));
            parentNode.setClazz("ClassificationNode");
            dto.setParentNode(parentNode);
        }
        ClassificationNodeViewDTO viewDTO = classificationNodeDelegator.create(dto);
        return new ReturnResult(ReturnCode.INSERT_OK,"新增分类基本信息成功",viewDTO.getId());
    }

    @Override
    public ReturnResult findOptionalAttrs() {
        QueryRequestVo queryRequestVo = new QueryRequestVo();
        QueryCondition queryCondition = new QueryCondition();
        queryCondition.addCondition("type",ConditionType.NOT_IN,Arrays.asList("CATEGORY", "REFERENCE_OBJECT"));
        queryCondition.addCondition("disableFlag",ConditionType.EQUAL,"false");
        queryCondition.setJoiner("and");
        queryRequestVo.setFilter(queryCondition);
        List<EXADefinitionQueryViewDTO> queryViewDTOS = exaDefinitionDelegator.query(queryRequestVo, new RDMPageVO());
        ArrayList<AttributeQueryViewVO> results = new ArrayList<>();
        for (EXADefinitionQueryViewDTO queryViewDTO : queryViewDTOS) {
            AttributeQueryViewVO vo = new AttributeQueryViewVO();
            BeanUtils.copyProperties(queryViewDTO,vo);
            vo.setId(queryViewDTO.getId().toString());
            results.add(vo);
        }
        return new ReturnResult(ReturnCode.GET_OK,"查询分类可选属性成功",results);
    }
    @Override
    public ReturnResult addClassificationAttrs(List<String> attrs, Long id) {
        if(attrs==null||attrs.isEmpty()){
            return new ReturnResult(ReturnCode.UPDATE_OK,"添加成功",0);
        }
        ArrayList<EXADefinitionLinkCreateDTO> dtoList = new ArrayList<>();
        for (String attr : attrs) {
            if(attr==null||attr.isEmpty())continue;
            EXADefinitionLinkCreateDTO linkCreateDTO = new EXADefinitionLinkCreateDTO();
            ObjectReferenceParamDTO source = new ObjectReferenceParamDTO();
            source.setClazz("EXADefinition");
            source.setId(Long.valueOf(attr));
            linkCreateDTO.setSource(source);
            ObjectReferenceParamDTO target = new ObjectReferenceParamDTO();
            target.setClazz("ClassificationNode");
            target.setId(id);
            linkCreateDTO.setTarget(target);
            dtoList.add(linkCreateDTO);
        }
        List<EXADefinitionLinkViewDTO> viewDTOS = exaDefinitionLinkDelegator.batchCreate(dtoList);
        return new ReturnResult(ReturnCode.UPDATE_OK,"增加属性成功",viewDTOS.size());
    }

}
